package com.example.tools.security.configure;

import com.example.tools.security.interceptor.ServerProtectInterceptor;
import com.example.tools.security.properties.MallSecurityProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Description:
 * 配置拦截器
 * @author Yang
 * create time 2021-08-25 0:38
 * @since 0.0.1
 */
@Component
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled=true)
@Order(SecurityProperties.BASIC_AUTH_ORDER - 10)
public class MallSecurityInterceptorConfigure implements  WebMvcConfigurer {
    private MallSecurityProperties properties;

    @Autowired
    public void setProperties(MallSecurityProperties properties) {
        this.properties = properties;
    }

    @Bean
    public HandlerInterceptor serverProtectInterceptor() {
        ServerProtectInterceptor interceptor = new ServerProtectInterceptor();
        interceptor.setProperties(properties);
        return interceptor;
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(serverProtectInterceptor());
    }
}
